package com.mapbased.wfw.components;

import java.io.*;
import java.util.*;

import org.dom4j.*;
import com.mapbased.wfw.*;
import com.mapbased.wfw.PageLoader.StringBufferBag;
import com.mapbased.wfw.PageLoader.LoadContext;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2004</p>
 *
 * <p>Company: mapbased.com</p>
 *
 * @author chy(Baily)
 * @version 1.0
 */
public class Condition
    extends Component
{
    public static final String ComponentName="condition";
    public static final String AN_When="when";
    public static final String AN_Invert="invert";
    private Binding when;
    private boolean invert=false;
    Object[] elseSegs=null;
    /**
     * parseXml
     *
     * @param ele Element
     * @todo Implement this com.mapbased.wfw.Component method
     */
    public Element parseXml(Element ele,
                            com.mapbased.wfw.PageLoader.LoadContext lc)
    {
        Attribute item=ele.attribute(AN_Invert);
        if(item != null)
        {
            this.invert=Boolean.valueOf(item.getValue().trim()).booleanValue();
            ele.remove(item);
        }

        Attribute a=ele.attribute(AN_When);
        if(a == null)
        {
            throw new java.lang.RuntimeException(
                "Cannot find when attribute for Condition:" +
                ele.getName());
        }
        String s=a.getValue();
        ele.remove(a);

        Element elseEle=ele.element("else");
        if(elseEle==null)
        {
            java.util.Iterator ei=ele.elementIterator();
          while(ei.hasNext())
          {
              Element e=(Element)ei.next();
              if("else".equalsIgnoreCase( e.attributeValue("jwcid")))
              {
                  elseEle=e;
                  break;
              }
          }
        }
        if(elseEle!=null)
        {
            LoadContext nlc=new LoadContext(lc);
            lc.loader.parseElementChildren(elseEle,nlc);
            nlc.finish();
            this.elseSegs=nlc.segments.toArray();
            ele.remove(elseEle);
        }


        PageLoader.LoadContext nlc=new PageLoader.LoadContext(lc);
        List l=nlc.segments;

        lc.loader.parseBinding(s,nlc);
        for(int i=0;i < l.size();i++)
        {
            Object o=l.get(i);
            if(o instanceof Binding)
            {
                this.when=(Binding)o;

                return ele;
            }
        }
        throw new java.lang.RuntimeException(
            "Cannot find binding for Condition:" +
            ele.getName());

    }

    public void renderHeader(RenderContext context,StringBuffer sb,Map m)
    {
    }

    /**
     * render
     *
     * @param context RenderContext
     * @throws IOException
     * @todo Implement this com.mapbased.wfw.Component method
     */
    public void render(RenderContext context)
        throws IOException
    {
        boolean b=true;
        Object o=when.getValue(context);
        if(o == null)
        {
            b=false;
        }
        else
        if(o instanceof Boolean)
        {
            b=((Boolean)o).booleanValue();
        }
        else if(o instanceof String)
        {
            b=!((String)o).trim().equals("");
        }
        else if(o instanceof Collection)
        {
            b=((Collection)o).size() > 0;
        }
        else if(o instanceof Map)
        {
            b=((Map)o).size() > 0;

        }
        else if(o instanceof Number)
        {
            b=((Number)o).intValue()>0;
        }
        b=this.invert ? !b : b;

        if(b)
        {
            this.defalutRenderBehavior(context);
        }
        else if(this.elseSegs!=null)
        {
             defalutRenderBehavior(context,this.elseSegs);
        }

    }

    public void setWhen(Binding when)
    {
        this.when=when;
    }

    public Binding getWhen()
    {
        return when;
    }
}
